﻿using RpcClient.Config.Model;
using RpcClient.Helper;
using RpcClient.Interface;
using RpcClient.Queue;
using RpcClient.Queue.Model;
using RpcClient.Track.Config;

using RpcHelper;
using RpcHelper.Config;
using RpcModel;

namespace RpcClient.Config
{
    internal class WebConfig
    {
        public static readonly string ApiVerNum = "1.0.3";

        public static int ApiVer = 0;
        private static LocalEnvironment _Environment;
        static WebConfig()
        {
            RpcClient.Config.AddRefreshEvent(_InitConfig);
            _Environment = new LocalEnvironment
            {
                Mac = LocalConfig.Local.GetValue("rpc:LocalMac", NetworkHelper.GetMac()),
                RunIsAdmin = Tools.IsAdminRun(),
                RunUserIdentity = Tools.GetRunUserIdentity()
            };
        }
        /// <summary>
        /// 获取Kafka配置
        /// </summary>
        /// <returns></returns>
        internal static KafkaConfig GetKafkaConfig()
        {
            KafkaConfig config = RpcClient.Config.GetConfigVal<KafkaConfig>("queue:Kafka");
            if (config == null || config.Servers.IsNull())
            {
                return null;
            }
            if (config.Public == null)
            {
                config.Public = new KafkaPublicConfig();
            }
            if (config.Consumer == null)
            {
                config.Consumer = new KafkaSubConfig();
            }
            return config;
        }

        /// <summary>
        /// 系统配置
        /// </summary>
        public static RpcSysConfig RpcConfig
        {
            get;
            private set;
        }

        /// <summary>
        /// 获取链路配置
        /// </summary>
        /// <returns></returns>
        internal static TrackConfig GetTrackConfig()
        {
            TrackConfig config = RpcClient.Config.GetConfigVal("rpcassembly:Track", new TrackConfig());
            if (config.ServiceName.IsNull())
            {
                config.ServiceName = Collect.RpcStateCollect.ServerConfig.Name;
            }
            if (config.TraceType == Track.TraceType.Local && config.Local == null)
            {
                config.Local = new LocalTrackConfig
                {
                    Dictate = "SysTrace",
                    SystemType = "sys.sync"
                };
            }
            return config;
        }
        /// <summary>
        /// 初始化配置项
        /// </summary>
        /// <param name="config"></param>
        /// <param name="name"></param>
        private static void _InitConfig(IConfigServer config, string name)
        {
            if (name == string.Empty || name.StartsWith("rpc:Config"))
            {
                RpcConfig = config.GetConfigVal<RpcSysConfig>("rpc:Config", new RpcSysConfig());
            }
            if (name == string.Empty || name.StartsWith("queue:QueueType"))
            {
                QueueType = LocalConfig.Local.GetValue("queue:QueueType", QueueType.RabbitMQ);
            }
        }

        #region 基础配置

        public static LocalEnvironment Environment => _Environment;

        /// <summary>
        /// 中控服务链接地址
        /// </summary>
        public static string[] RpcServer => LocalConfig.Local["rpc:RpcServer", typeof(string[])];
        /// <summary>
        /// 集群AppId
        /// </summary>
        public static string AppId => LocalConfig.Local["rpc:RpcAppId"];
        /// <summary>
        /// 集群链接密钥
        /// </summary>
        public static string AppSecret => LocalConfig.Local["rpc:RpcAppSecret"];
        /// <summary>
        /// 服务节点类型
        /// </summary>
        public static string RpcSystemType => LocalConfig.Local["rpc:RpcSystemType"];

        /// <summary>
        /// 服务节点索引
        /// </summary>
        public static int RpcServerIndex => LocalConfig.Local.GetValue("rpc:RpcIndex", 0);

        /// <summary>
        /// 是否启用错误管理
        /// </summary>
        public static bool IsEnableError => LocalConfig.Local.GetValue("rpc:IsEnableError", true);

        public static QueueType QueueType { get; private set; }

        #endregion



        public static RabbitmqConfig GetRabbitmqConfig()
        {
            RabbitmqConfig config = RpcClient.Config.GetConfigVal<RabbitmqConfig>("queue:Rabbitmq");
            if (config == null || config.Servers.IsNull())
            {
                return null;
            }
            else if (config.Expiration == 0)
            {
                config.Expiration = RpcConfig.ExpireTime * 1000;
            }
            return config;
        }

        public static LogConfig GetLogConfig()
        {
            return RpcClient.Config.GetConfigVal<LogConfig>("rpc:Log", new LogConfig());
        }

    }
}
